/**
 * @author Isabella Nake (isabella.nake@usi.ch)
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.gui.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import ch.usi.inf.pf2.circuit.Input;
import ch.usi.inf.pf2.gui.CircuitFrame;
import ch.usi.inf.pf2.gui.Model;
import ch.usi.inf.pf2.time.Schedule;

public class RunAction extends AbstractAction {
	
	private static final long serialVersionUID = 4793792582821189819L;
	private final Model model;
	private final CircuitFrame circuitFrame;


    public RunAction(final CircuitFrame circuitFrame, final Model model) {
    	this.circuitFrame = circuitFrame;
        this.model = model;
        putValue(NAME, "Run");
        final ImageIcon icon = new ImageIcon(getClass().getResource("../icons/run.png"));
        putValue(SMALL_ICON, icon);
        putValue(SHORT_DESCRIPTION, "Run the circuit.");

    }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		model.clearSelection();
		Schedule.resetStep();
		Schedule.run();
		Schedule.resetStep();
		model.shapeChanged();
		
		ArrayList<Input> inputs = model.getInputs();
		circuitFrame.getEditor().removeAll();
		
		for(Input input : inputs) {
			if(input.getWaveform() != "") {
				char currentValue2 = input.getWaveform().charAt(input.getWaveform().length()-1); //this gives the last digit of waveform
				circuitFrame.setEditor(new JLabel("<html><font>" +input.getName() + ": " +"</font><font>"+input.getWaveform().substring(0, input.getWaveform().length() -1)
						+"</font><span style=\"BACKGROUND-COLOR: #32CD32\">" + currentValue2 + "</span></html>"));
			}
		}
		circuitFrame.getEditor().revalidate();
		
	}

}
